#include "function.h"

using namespace std;

void showmenu()
{
    cout << "***********************" << endl;
    cout << "*****1、添加联系人*****" << endl;
    cout << "*****2、显示联系人*****" << endl;
    cout << "*****3、删除联系人*****" << endl;
    cout << "*****4、查找联系人*****" << endl;
    cout << "*****5、修改联系人*****" << endl;
    cout << "*****6、清空联系人*****" << endl;
    cout << "*****0、退出通讯录*****" << endl;
    cout << "***********************" << endl;
}

void addperson(struct addressbook * abs)
{
    if(abs->size == PERSON_MAX)
    {
        cout << "通讯录已满，无法添加！" << endl;
        return;
    }
    else
    {
        cout << "请输入姓名：" << endl;
        cin >> abs->personarr[abs->size].name;
        cout << "请输入性别：" << endl;
        cout << "1 --- 男" << endl;
        cout << "2 --- 女" << endl;
        int sex = 0;
        cin >> sex;
        while (1)
        {
            if(sex == 1 || sex == 2)
            {
                abs->personarr[abs->size].sex = sex;
                break;
            }
            else
            {
                cout << "输入有误，请重新输入" << endl;
                cin >> sex;
            }
        }
        cout << "请输入年龄：" << endl;
        cin >> abs->personarr[abs->size].age;
        cout << "请输入电话：" << endl;
        cin >> abs->personarr[abs->size].phone;
        cout << "请输入住址：" << endl;
        cin >> abs->personarr[abs->size].addr;
        abs->size++;

        cout << "添加成功！" << endl;
    }
}

void showperson(struct addressbook * abs)
{
    if (abs->size == 0)
    {
        cout << "当前记录为空" << endl;
        return;
    }
    else
    {
        for (int i = 0; i < abs->size; i ++)
        {
            cout << "姓名：" << abs->personarr[i].name << "\t";
            cout << "性别：" << (abs->personarr[i].sex == 1 ? "男" : "女") << "\t";
            cout << "年龄：" << abs->personarr[i].age << "\t";
            cout << "电话：" << abs->personarr[i].phone << "\t";
            cout << "住址：" << abs->personarr[i].addr << "\t";
            cout << endl;
        }
    }
}

void deleteperson(struct addressbook * abs)
{
    if(abs->size == 0)
    {
        cout << "当前记录为空";
        return;
    }
    cout << "请输入删除联系人姓名：" << endl;
    string name;
    int num;
    char flag = 0;
    cin >> name;
    for (int i = 0; i < abs->size; i++)
    {
        if(abs->personarr[i].name == name)
        {
            cout << "姓名：" << abs->personarr[i].name << "\t";
            cout << "性别：" << (abs->personarr[i].sex == 1 ? "男" : "女") << "\t";
            cout << "年龄：" << abs->personarr[i].age << "\t";
            cout << "电话：" << abs->personarr[i].phone << "\t";
            cout << "住址：" << abs->personarr[i].addr << "\t";
            cout << endl;
            cout << "删除的是这个人吗？（删除请输入'd'）" << endl;
            std::cin >> flag;
            if(flag == 'd')
            {
                for (; i < abs->size - 1; i++)
                {
                    abs->personarr[i] = abs->personarr[i + 1];
                }
                cout << "删除成功！";
                abs->size--;
                return;
            }
        }
    }
    cout << "查无此人！" << endl;
}

void findperson(struct addressbook * abs)
{
    if(abs->size == 0)
    {
        cout << "当前记录为空";
        return;
    }
    string name;
    char flag = 0;
    cout << "请输入查找联系人姓名：" << endl;
    cin >> name;
    for (int i = 0; i < abs->size; i++)
    {
        if(abs->personarr[i].name == name)
        {
            cout << "姓名：" << abs->personarr[i].name << "\t";
            cout << "性别：" << (abs->personarr[i].sex == 1 ? "男" : "女") << "\t";
            cout << "年龄：" << abs->personarr[i].age << "\t";
            cout << "电话：" << abs->personarr[i].phone << "\t";
            cout << "住址：" << abs->personarr[i].addr << "\t";
            cout << endl;
            cout << "修改的是这个人吗？（是请输入'y'）" << endl;
            cin >> flag;
            if(flag == 'y')
            {
                cout << "查找成功" << endl;
                return;
            }
        }
    }
    cout << "查无此人！" << endl;
}

void changeperson(struct addressbook *abs)
{
    if(abs->size == 0)
    {
        cout << "当前记录为空";
        return;
    }
    string name;
    char flag = 0;
    cout << "请输入查找联系人姓名：" << endl;
    cin >> name;
    for (int i = 0; i < abs->size; i++)
    {
        if(abs->personarr[i].name == name)
        {
            cout << "姓名：" << abs->personarr[i].name << "\t";
            cout << "性别：" << (abs->personarr[i].sex == 1 ? "男" : "女") << "\t";
            cout << "年龄：" << abs->personarr[i].age << "\t";
            cout << "电话：" << abs->personarr[i].phone << "\t";
            cout << "住址：" << abs->personarr[i].addr << "\t";
            cout << endl;
            cout << "修改的是这个人吗？（是请输入'y'）" << endl;
            cin >> flag;
            if(flag == 'y')
            {
                cout << "请输入姓名：" << endl;
                cin >> abs->personarr[i].name;
                cout << "请输入性别：" << endl;
                cout << "1 --- 男" << endl;
                cout << "2 --- 女" << endl;
                int sex = 0;
                cin >> sex;
                while (1)
                {
                    if(sex == 1 || sex == 2)
                    {
                       abs->personarr[i].sex = sex;
                       break;
                    }
                    else
                    {
                        cout << "输入有误，请重新输入" << endl;
                        cin >> sex;
                    }
                }
                cout << "请输入年龄：" << endl;
                cin >> abs->personarr[i].age;
                cout << "请输入电话：" << endl;
                cin >> abs->personarr[i].phone;
                cout << "请输入住址：" << endl;
                cin >> abs->personarr[i].addr;
                cout << "修改成功" << endl;
                return;
            }
        }
    }
    cout << "查无此人！" << endl;
}

void cleanperson(struct addressbook * abs)
{
    abs->size = 0;
    cout << "通讯录已清空" << endl;
}

void clear()
{
    cout << "是否清屏？（清屏请按'c'）" << endl;
    char clear;
    cin >> clear;
    if (clear == 'c')
    {
        system("clear");
        showmenu();
    }
    else
    {
        showmenu();
    }
}